/*
 * PGN_Reader : A simple pgn reader for chess games.
 * Copyright (C) 2011 Laurent BERNABE
 * 
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package com.gmail.bernabe.laurent.java_swing.pgn_reader.processing;

import com.gmail.bernabe.laurent.java_swing.pgn_reader.processing.move.BoardCell;
import com.gmail.bernabe.laurent.java_swing.pgn_reader.processing.piece.PieceForce;

public class HalfMoveData {
	
	public HalfMoveData(String value){
		halfMoveValue = value;
	}
	
	public String getValue(){
		return halfMoveValue;
	}
	
	public boolean isLittleRoque(){
		return halfMoveValue.startsWith("O-O");
	}

	public boolean isBigRoque(){
		return halfMoveValue.startsWith("O-O-O");
	}

	public PieceForce movingPieceForce(){
		PieceForce pieceToMove_force;
		boolean pieceIsNotAPawn = halfMoveValue.toString().matches("^(N|B|R|Q|K).*");
		if (pieceIsNotAPawn){
			pieceToMove_force = PieceForce.getPieceForceFromUppercaseInitial(
					halfMoveValue.charAt(0));
		}
		else
			pieceToMove_force = PieceForce.pawn;

		return pieceToMove_force;
	}

	
	public boolean isAChessOrChessMate(){
		return halfMoveValue.matches(".*(#|\\+)$");
	}

	public boolean isAPromotionMove(){
		String halfMoveWithoutChessNorChessmate = halfMoveValue;
		if (isAChessOrChessMate()){
			halfMoveWithoutChessNorChessmate = removeLastCharacterFrom(halfMoveValue);
		}
		return halfMoveWithoutChessNorChessmate.matches(".*=(B|N|R|Q)$");
	}
	
	public boolean isACapturingMove(){
		return halfMoveValue.contains("x");
	}
	
	public boolean hasAForcedRowOrColumnStart() {
		String halfMoveWithoutPromotionNorChessStatus = 
			getHalfMoveWithoutPromotionNorChessStatus();
		boolean hasAForcedRowOrColumnStart = 
			(halfMoveWithoutPromotionNorChessStatus.length() >= 4 && !isACapturingMove()) ||
			(halfMoveWithoutPromotionNorChessStatus.length() >= 5 && isACapturingMove() );
		return hasAForcedRowOrColumnStart;
	}

	public boolean isAPawnMove() {
		boolean isAPawnMove =  ! halfMoveValue.matches("^(N|B|R|Q|K).*");
		return isAPawnMove;
	}
	
	public int forcedStartRowOrColumn(){
		int stateToReturn;
		if ( isAPawnMove() && isACapturingMove() ){
			Character startColumn = halfMoveValue.charAt(0);
			stateToReturn = startColumn.compareTo('a');
		}
		else if (hasAForcedRowOrColumnStart()) {
			Character guiltyColumnOrRow = halfMoveValue.charAt(1);
			if (Character.isDigit(guiltyColumnOrRow)){
				stateToReturn = guiltyColumnOrRow.compareTo('0');
			}
			else {
				stateToReturn = guiltyColumnOrRow.compareTo('a');
			}
		}
		else {
			stateToReturn = -1;
		}
		return stateToReturn;
	}
	
	public boolean isAStartLineForced(){
		boolean stateToReturn;
		if (hasAForcedRowOrColumnStart()){
			boolean isSecondCharacterADigit = halfMoveValue.matches("^.\\d.*");
			stateToReturn = isSecondCharacterADigit;
		}
		else {
			stateToReturn = false;
		}
		return stateToReturn;
	}

	public boolean isAStartColumnForced(){
		boolean stateToReturn;
		if (isAPawnMove() && isACapturingMove()){
			stateToReturn = true;
		}
		else if (hasAForcedRowOrColumnStart()){
			boolean isSecondCharacterAValidLetter = halfMoveValue.matches("^.[a-h].*");
			stateToReturn = isSecondCharacterAValidLetter;
		} 
		else {
			stateToReturn = false;
		}
		return stateToReturn;
	}
	
	public BoardCell getEndCell(){

		String halfMoveWithoutPromotionNorChessStatus = 
			getHalfMoveWithoutPromotionNorChessStatus();

		int stringLength = halfMoveWithoutPromotionNorChessStatus.length();
		Character column = halfMoveWithoutPromotionNorChessStatus.charAt(stringLength-2);
		Character line = halfMoveWithoutPromotionNorChessStatus.charAt(stringLength-1);
		return new BoardCell(line.compareTo('1'), column.compareTo('a'));
	}

	private String getHalfMoveWithoutPromotionNorChessStatus() {
		String halfMoveWithoutPromotionNorChessStatus = halfMoveValue;
		if (isAChessOrChessMate()){
			halfMoveWithoutPromotionNorChessStatus =
				removeLastCharacterFrom(halfMoveWithoutPromotionNorChessStatus);
		}
		if (isAPromotionMove()){
			halfMoveWithoutPromotionNorChessStatus = 
				removeLastCharacterFrom(halfMoveWithoutPromotionNorChessStatus);
			halfMoveWithoutPromotionNorChessStatus = 
				removeLastCharacterFrom(halfMoveWithoutPromotionNorChessStatus);
		}
		return halfMoveWithoutPromotionNorChessStatus;
	}
	
	private String removeLastCharacterFrom(String stringToConvert){
		return stringToConvert.substring(0, stringToConvert.length()-1);
	}
	
	private String halfMoveValue;

}
